package com.wctj.api.web.config;

import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger 配置
 */
@Configuration
@Profile({"local", "dev", "DEV", "fat", "FAT", "UAT"})
@EnableOpenApi
public class SwaggerConfig implements WebMvcConfigurer {
    @Bean
    public Docket createRestApi() {
        List<Response> responseList = new ArrayList<>();
        return new Docket(DocumentationType.OAS_30)
                .globalRequestParameters(requestParameterBuilderList())
                .apiInfo(apiInfo())
                .select()
                // 扫描所有有注解的api
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .apis(RequestHandlerSelectors.basePackage("com.tmzh.saas"))
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("网站模块接口文档")
                .description("网站模块接口文档：do something表示测试接口")
                .version("1.0.0")
                .build();
    }

    private ArrayList<RequestParameter> requestParameterBuilderList(){
        return Lists.newArrayList(
                new RequestParameterBuilder()
                        .name("X-tmy-username")
                        .description("username")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("nbg@8531.com")
                                .allowEmptyValue(false))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-realName")
                        .description("realName")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("大洋测试1")
                                .allowEmptyValue(false))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-tenantId")
                        .description("tenantId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("102")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-userId")
                        .description("userId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("419")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-groupId")
                        .description("groupId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("0")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-userType")
                        .description("userType")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("0")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-systemCode")
                        .description("systemCode")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("TMY")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-idsGroupId")
                        .description("idsGroupId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("0,1,2")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("Authorization")
                        .description("token")
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("bearer abc")
                                .allowEmptyValue(true))
                        .in(ParameterType.HEADER)
                        .build());
    }
}